package com.yugao.fintech.antelope.statistic.controller;

import com.yugao.fintech.antelope.base.model.module.tenant.TenantContextHolder;
import com.yugao.fintech.antelope.base.ram.ActionType;
import com.yugao.fintech.antelope.base.ram.RamResource;
import com.yugao.fintech.antelope.statistic.api.constant.StatisticCons;
import com.yugao.fintech.antelope.statistic.handler.AbstractStatisticHandler;
import com.yugao.fintech.antelope.statistic.model.entity.StatLogin;
import com.yugao.fintech.antelope.statistic.model.rqrs.StatLoginListReq;
import com.yugao.fintech.antelope.statistic.service.StatLoginService;
import com.yugao.fintech.framework.core.response.Resp;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 统计登录与登出数据
 */
@RestController
@RequestMapping("/statistic/login")
@Api(tags = "统计登录与登出数据")
public class StatLoginController {
    @Autowired
    private StatLoginService statLoginService;

    @ApiOperation(value = "查询统计统计登录与登出数据")
    @GetMapping("/list")
    @RamResource(action = "statistic:login:query", name = "查询统计登录与登出", type = ActionType.READ)
    public Resp<List<StatLogin>> listStatLogin(StatLoginListReq req) {
        return Resp.ok(statLoginService.listStatLogin(req));
    }

    /**
     * 删除多少天之前的日志
     * @param beforeDays 天数
     */
    @ApiOperation(value = "删除统计的访问日志")
    @DeleteMapping("/deleteTask")
    public Resp<?> deleteStatLogin(@RequestParam(value = "beforeDays") Long beforeDays) {
        return Resp.ok(statLoginService.deleteStatLogin(beforeDays));
    }

    @ApiOperation(value = "统计任务")
    @PostMapping("/statTask")
    public Resp<?> statTask() {
        AbstractStatisticHandler.getHandler(StatisticCons.Type.LOGIN_LOGOUT).execTask(TenantContextHolder.get().getTenantId());
        return Resp.ok();
    }
}
